//
//  HomeViewCell.m
//  LoveBeeProject
//
//  Created by skywalker on 17/1/9.
//  Copyright © 2017年 斯芬克斯. All rights reserved.
//

#import "HomeViewCell.h"
#import<UIImageView+WebCache.h>
@interface HomeViewCell()
///图片
@property (weak, nonatomic) UIImageView *img;
///规格
@property (weak, nonatomic) UILabel *specifics;
///价格
@property (weak, nonatomic) UILabel *partner_price;
///id
@property (copy, nonatomic) NSString *tag_ids;
///优惠
@property (weak, nonatomic) UILabel *pm_desc;
///名字
@property (weak, nonatomic) UILabel *name;
///选购数量
@property (weak, nonatomic) UILabel *shopNumber;

@end
@implementation HomeViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
    
}

- (void)setupUI {
    //创建图片控件
    UIImageView *img = [[UIImageView alloc] init];
    self.img = img;
    img.contentMode = UIViewContentModeScaleAspectFit;
    
    [self.contentView addSubview:img];
    
    //创建图片控件
    UIImageView *imageV = [[UIImageView alloc] init];
    imageV.image = [UIImage imageNamed:@"jingxuan.png-2"];
    
    [self.contentView addSubview:imageV];
    
    //创建图片控件
    UIImageView *imageOne = [[UIImageView alloc] init];
    imageOne.image = [UIImage imageNamed:@"buyOne.png-2"];
    
    [self.contentView addSubview:imageOne];
    
    
    UILabel *name = [[UILabel alloc] init];
    self.name = name;
    name.font = [UIFont systemFontOfSize:13];
    
    [self.contentView addSubview:name];
    
    ///规格
    UILabel *specifics = [[UILabel alloc] init];
    self.specifics = specifics;
    specifics.font = [UIFont systemFontOfSize:10];
    
    [self.contentView addSubview:specifics];
    
    ///价格
    UILabel *partner_price = [[UILabel alloc] init];
    self.partner_price = partner_price;
    partner_price.font = [UIFont systemFontOfSize:10];
    
    [self.contentView addSubview:partner_price];
    
    UILabel *pm_desc = [[UILabel alloc] init];
    self.pm_desc = pm_desc;
    
    [self.contentView addSubview:pm_desc];
    
    //添加
    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateHighlighted];
    
    [self.contentView addSubview:btn];
    
    // 添加监听
    // 按钮的点击方法
    [btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 添加数量label
    UILabel *shopNumber = [[UILabel alloc] init];
    self.shopNumber = shopNumber;
    shopNumber.font = [UIFont systemFontOfSize:12];
    shopNumber.hidden = YES;
    
    [self.contentView addSubview:shopNumber];
    
    // 设置剑豪按钮
    UIButton *minusSignBtn = [[UIButton alloc] init];
    [minusSignBtn setImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
    self.minusSignBtn = minusSignBtn;
    minusSignBtn.hidden = YES;
    
    [self.contentView addSubview:minusSignBtn];
    
    //设置减号监听事件
    [minusSignBtn addTarget:self action:@selector(minusSignBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    //约束
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(4);
        make.bottom.right.equalTo(self.contentView).offset(-4);
    }];
    
    //    //设置价格约束
    //    [partner_price mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.bottom.equalTo(img).offset(1);
    //        make.left.equalTo(img).offset(1);
    //    }];
    
    //设置规格约束
    [specifics mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img).offset(2);
        make.bottom.equalTo(img).offset(-1);
    }];
    
    //设置精选约束
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(specifics.mas_top).offset(-1);
        make.left.equalTo(specifics);
        make.width.offset(20);
        make.height.offset(10);
    }];
    
    //设置买一赠一约束
    [imageOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageV.mas_right).offset(2);
        make.centerY.equalTo(imageV);
        make.width.offset(20);
        make.height.offset(10);
    }];
    
    //设置名字
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(imageV.mas_top).offset(-1);
        make.left.equalTo(imageV);
        make.right.equalTo(img).offset(-1);
    }];
    
    //设置约束
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(img).offset(-2);
        make.bottom.equalTo(img).offset(-4);
        make.width.height.offset(20);
    }];
    
    //设置数量lable
    [shopNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(btn.mas_left).offset(-3);
        make.centerY.equalTo(btn);
    }];
    
    //设置减号约束
    [minusSignBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(shopNumber.mas_left).offset(-3);
        make.centerY.equalTo(shopNumber);
        make.width.height.offset(20);
    }];
}

-(void)setModel:(OpHomeModel *)model {
    _model = model;
    
    [self.img sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"icon_icons_holder"]];
    
    self.name.text = model.name;
    self.specifics.text = model.specifics;
    self.partner_price.text = model.partner_price;
    
    self.shopNumber.text = @"1";
}

//加号按钮点击事件
- (void)buttonClick {
    
    self.shopNumber.hidden = NO;
    self.minusSignBtn.hidden = NO;
    _model.goodNumbers += 1;
    self.shopNumber.text = [NSString stringWithFormat:@"%ld",(long)_model.goodNumbers];
    
    // 判断
    if ([self.delegate respondsToSelector:@selector(HomeViewCell:withImageView:)])
    {
        [self.delegate HomeViewCell:self withImageView:self.img];
    }
    
}
//减号按钮监听事件
- (void)minusSignBtnClick {
    _model.goodNumbers -= 1;
    self.shopNumber.text = [NSString stringWithFormat:@"%ld",(long)_model.goodNumbers];
    if (_model.goodNumbers < 1) {
        self.shopNumber.hidden = YES;
        self.minusSignBtn.hidden = YES;
        
    }
    if ([self.delegate respondsToSelector:@selector(HomeViewCell:withButton:)]) {
        
        [self.delegate HomeViewCell:self withButton:self.minusSignBtn];
        
    }
    
}

@end
